package logic.common.missions;

import logic.common.player.Player;
import logic.common.team.Team;
import logic.common.team.teamListeners.DroneKillEvent;
import logic.common.team.teamListeners.ShipAddedEvent;
import logic.common.team.teamListeners.ShipRemovedEvent;
import logic.common.team.teamListeners.TeamListener;
import logic.common.team.teamListeners.TeamShipListener;
import logic.common.team.teamListeners.TeamValueChangedEvent;
import logic.nodes.DestructionEvent;
import logic.nodes.DestructionListener;
import logic.ships.drone.Drone;
import logic.ships.resourceShip.ResourceShip;

import org.fenggui.util.Color;

import com.jme.math.Vector3f;

import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.HUDText;

public class DroneHarassMission extends Mission {
	
	protected ResourceShip ship;
	protected DestructionListener destructionListener;
	protected TeamListener teamListener;
	protected TeamShipListener teamShipListener;
	
	public DroneHarassMission(Team team, int money, int assistMoney, ResourceShip ship) {
		super(team, money, assistMoney);
		assert(team != ship.getTeam());
		this.ship = ship;
		
		addListener();
	}
	
	protected void addListener() {
		destructionListener = new DestructionListener() {
			@Override
			public void destroyed(DestructionEvent event) {
				team.deleteMission(DroneHarassMission.this);
				ship.removeDestructionListener(destructionListener);
			}
		};
		ship.addDestructionListener(destructionListener);
		
		teamListener = new TeamListener() {
			@Override
			public void droneDestroyed(DroneKillEvent event) {
				Player player = event.getKiller();
				if(player.getTeam() == team) {
					missionAccomplished(player);
					
					Drone drone = (Drone)event.getShip();
					for(Player p : drone.getShooters()) {
						if(p != player) p.assist(DroneHarassMission.this);
					}
				}
			}
			
			@Override
			public void playerRemoved(Player player) {}
			@Override
			public void playerAdded(Player player) {}
			@Override
			public void teamValueChanged(TeamValueChangedEvent event) {}
		};
		ship.getTeam().addTeamListener(teamListener);
		
		teamShipListener = new TeamShipListener() {
			@Override
			public void shipRemoved(ShipRemovedEvent event) {
				if(event.getShip() == ship) removeListeners();
			}
			
			@Override
			public void shipAdded(ShipAddedEvent event) {}
		};
	}
	
	private void removeListeners() {
		team.deleteMission(DroneHarassMission.this);
		ship.getTeam().removeTeamListener(teamListener);
		ship.getTeam().removeTeamShipListener(teamShipListener);
	}
	
	@Override
	public void missionActivated(boolean active, Player player) {
		super.missionActivated(active, player);
		if(player == team.getIngameState().getPlayer()) ship.activateMissionAngles(active);
	}
	
	@Override
	protected String getText() {
		return LanguageLoader.get(HUDText.Destroy_Drones);
	}
	
	@Override
	public String[] getInfoTexts() {
		return new String[] { LanguageLoader.get(HUDText.Drone_Destroyed) };
	}
	
	@Override
	public Color[] getInfoColors() {
		return new Color[] { Color.WHITE };
	}
	
	@Override
	public String[] getAssistTexts() {
		return new String[] { LanguageLoader.get(HUDText.Drone_Kill_Assist) };
	}
	
	@Override
	public Color[] getAssistColors() {
		return new Color[] { Color.WHITE };
	}
	
	@Override
	public Vector3f getMissionLocation() { return ship.getLocalTranslation(); }
	
	@Override
	public boolean deleteAfterComplete() { return false; }
}